﻿using Dexter.CallContext;
using Dexter.Core.Data.Repository;
using Dexter.Dependency;
using Dexter.Dependency.Installation;
using Dexter.Repository.NHibernate.Configuration;
using Dexter.Repository.NHibernate.Helpers;
using Dexter.Repository.NHibernate.Repositories;
using Dexter.Repository.NHibernate.SessionFactory;

namespace Dexter.Repository.NHibernate.Installer {
	public class NHibernateInstaller : ILayerInstaller{
		/// <summary>
		/// 	Installs all the component needed by the assembly.
		/// </summary>
		/// <param name = "container">The container.</param>
		public void ServiceRegistration ( IDexterContainerRegister container ) {
			container.Register<ISessionFactory, SessionFactory.SessionFactory> ( "SessionFactory", LifeCycle.Singleton );
			container.Register<INHibernateHelper, NHibernateHelper> ( "NHibernateHelper", LifeCycle.Singleton );
			container.Register<IBlogRollRepository, BlogRollRepository> ( "BlogRollRepository", LifeCycle.Singleton );
			container.Register<ICategoryRepository, CategoryRepository> ( "CategoryRepository", LifeCycle.Singleton );
			container.Register<ICommentRepository, CommentRepository> ( "CommentRepository", LifeCycle.Singleton );
			container.Register<IConfigurationRepository, ConfigurationRepository> ( "ConfigurationRepository", LifeCycle.Singleton );
			container.Register<IEmailRepository, EmailRepository> ( "EmailRepository", LifeCycle.Singleton );
			container.Register<IPageRepository, PageRepository> ( "PageRepository", LifeCycle.Singleton );
			container.Register<IPostRepository, PostRepository> ( "PostRepository", LifeCycle.Singleton );
			container.Register<IReferrerRepository, ReferrerRepository> ( "ReferrerRepository", LifeCycle.Singleton );
			container.Register<ITrackBackRepository, TrackBackRepository> ( "TrackBackRepository", LifeCycle.Singleton );
			container.Register<ILegacyUrlRepository, LegacyUrlRepository>("LegacyUrlRepository", LifeCycle.Singleton);
			container.Register<IUserRepository, UserRepository> ( "UserRepository", LifeCycle.Singleton );
			container.Register<ITagRepository, TagRepository> ( "TagRepository", LifeCycle.Singleton );
			container.Register<IPluginRepository, PluginRepository>("PluginRepository", LifeCycle.Singleton);
			container.Register<IRoleRepository, RoleRepository> ( "RoleRepository", LifeCycle.Singleton );
			container.Register<IInboxRepository, InboxRepository> ( "InboxRepository", LifeCycle.Singleton );
			container.Register<IGlobalRepository, GlobalRepository> ( "GlobaRepository", LifeCycle.Singleton );
			container.Register<IDexterCall, DexterCall> ( "DexterCall", LifeCycle.Singleton );
			container.Register<INHibernateConfiguration, NHibernateConfiguration> ( "NHibernateConfiguration", LifeCycle.Singleton );
		}

		/// <summary>
		/// 	Setups the service.
		/// </summary>
		/// <param name = "container">The container.</param>
		public void ServiceRegistrationComplete ( IDexterContainerResolver container ) {
			var helper = container.Resolve<INHibernateHelper> ( );
			
			helper.Configure ( );
		}

		/// <summary>
		/// 	Setups the service.
		/// </summary>
		/// <param name = "container">The container.</param>
		public void ApplicationStarted ( IDexterContainerResolver container ) {
		}
	}
}
